package com.hanlin.backend.common.util;

import com.hanlin.backend.common.dto.MailBase;
import com.hanlin.backend.common.error.CommonException;
import com.hanlin.backend.common.error.ErrorCodeEnum;
import com.hanlin.backend.common.service.HlServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * 邮件发送util
 *
 * @author:hl.yuan
 * @date:2021-01-26
 */
@Slf4j
@Component
public class MailUtil {
    /**
     * 邮件发送：文本信息
     *
     * @param mailBase
     */
    public static void mailSendText(MailBase mailBase) throws CommonException {
        JavaMailSender mailSender = SpringContextUtil.getBean(JavaMailSender.class);
        SimpleMailMessage message = new SimpleMailMessage();

        String username = HlServiceFactory.getSettingService().getSetting("COMMON", "MAILFROM", "17386677278@163.com");

        // 发件人
        message.setFrom(username);
        // 收件人
        message.setTo(listToString(mailBase.getReceivers()));
        // 邮件标题
        message.setSubject(mailBase.getSubject());
        // 邮件内容
        message.setText(mailBase.getContent());
        List<String> copies = mailBase.getCopies();
        if (copies != null && copies.size() > 0) {
            // 抄送人
            message.setCc(listToString(copies));
        }
        try {
            mailSender.send(message);
            log.info("邮件发送成功！");
        } catch (Exception ex) {
            log.error("邮件发送失败", ex);
            throw new CommonException(ErrorCodeEnum.SYS_ERROR, "邮件发送失败");
        }

    }

    /**
     * 邮件发送：文本信息(html)
     *
     * @param mailBase
     */
    public static void mailSendHtml(MailBase mailBase) throws CommonException {
        JavaMailSender mailSender = SpringContextUtil.getBean(JavaMailSender.class);
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            String username = HlServiceFactory.getSettingService().getSetting("COMMON", "MAILFROM", "17386677278@163.com");

            // 发件人
            helper.setFrom(username);
            // 收件人
            helper.setTo(listToString(mailBase.getReceivers()));
            // 邮件标题
            helper.setSubject(mailBase.getSubject());
            // 邮件内容
            helper.setText(mailBase.getContent(), true);
            List<String> copies = mailBase.getCopies();
            if (copies != null && copies.size() > 0) {
                // 抄送人
                helper.setCc(listToString(copies));
            }
            mailSender.send(message);
            log.info("邮件发送成功！");
        } catch (Exception ex) {
            log.error("邮件发送失败", ex);
            throw new CommonException(ErrorCodeEnum.SYS_ERROR, "邮件发送失败");
        }

    }

    /**
     * 邮件发送：带有附件的
     *
     * @param mailBase
     */
    public static void mailSendAttachment(MailBase mailBase) {
        JavaMailSender mailSender = SpringContextUtil.getBean(JavaMailSender.class);
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            String username = HlServiceFactory.getSettingService().getSetting("COMMON", "MAILFROM", "17386677278@163.com");

            helper.setFrom(username);
            helper.setTo(listToString(mailBase.getReceivers()));
            helper.setSubject(mailBase.getSubject());
            helper.setText(mailBase.getContent());
            List<String> copies = mailBase.getCopies();
            if (copies != null && copies.size() > 0) {
                // 抄送人
                helper.setCc(listToString(copies));
            }
            List<File> attachments = mailBase.getAttachments();
            if (attachments != null && attachments.size() > 0) {
                for (File file : attachments) {
                    helper.addAttachment(file.getName(), file);
                }
            }
            mailSender.send(message);
            log.info("邮件发送成功！");
        } catch (Exception e) {
            log.info("邮件发送失败");
            throw new CommonException(ErrorCodeEnum.SYS_ERROR, "邮件发送失败");
        }
    }

    public static String[] listToString(List<String> list) {
        if (list != null) {
            String[] strings = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                strings[i] = list.get(i);
            }
            return strings;
        }
        return null;
    }

}
